<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.8"/>
<title>AndBase开发框架: com.google.gson.TypeAdapter&lt; T &gt;类 参考</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="ic_launcher.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AndBase开发框架
   &#160;<span id="projectnumber">1.6</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.8.8 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'搜索');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>首页</span></a></li>
      <li><a href="pages.html"><span>相关页面</span></a></li>
      <li><a href="namespaces.html"><span>命名空间</span></a></li>
      <li class="current"><a href="annotated.html"><span>类</span></a></li>
      <li><a href="files.html"><span>文件</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="搜索" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>类列表</span></a></li>
      <li><a href="classes.html"><span>类索引</span></a></li>
      <li><a href="hierarchy.html"><span>类继承关系</span></a></li>
      <li><a href="functions.html"><span>类成员</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>全部</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>类</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>命名空间</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>文件</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>函数</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>变量</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>枚举值</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>页</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="namespacecom.html">com</a></li><li class="navelem"><a class="el" href="namespacecom_1_1google.html">google</a></li><li class="navelem"><a class="el" href="namespacecom_1_1google_1_1gson.html">gson</a></li><li class="navelem"><a class="el" href="classcom_1_1google_1_1gson_1_1_type_adapter_3_01_t_01_4.html">TypeAdapter< T ></a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">com.google.gson.TypeAdapter&lt; T &gt;类 参考<span class="mlabels"><span class="mlabel">abstract</span></span></div>  </div>
</div><!--header-->
<div class="contents">
<div class="dynheader">
类 com.google.gson.TypeAdapter&lt; T &gt; 继承关系图:</div>
<div class="dyncontent">
 <div class="center">
  <img src="classcom_1_1google_1_1gson_1_1_type_adapter_3_01_t_01_4.png" usemap="#com.google.gson.TypeAdapter&lt; T &gt;_map" alt=""/>
  <map id="com.google.gson.TypeAdapter&lt; T &gt;_map" name="com.google.gson.TypeAdapter&lt; T &gt;_map">
<area href="classcom_1_1google_1_1gson_1_1_tree_type_adapter_3_01_t_01_4.html" alt="com.google.gson.TreeTypeAdapter&lt; T &gt;" shape="rect" coords="0,56,353,80"/>
<area href="classcom_1_1google_1_1gson_1_1_tree_type_adapter_3_01_t_01_4_1_1_single_type_factory.html" alt="com.google.gson.TreeTypeAdapter&lt; T &gt;.SingleTypeFactory" shape="rect" coords="0,112,353,136"/>
</map>
 </div></div>
<a name="details" id="details"></a><h2 class="groupheader">详细描述</h2>
<div class="textblock"><p>Converts Java objects to and from JSON.</p>
<h3>Defining a type's JSON form</h3>
<p>By default <a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a> converts application classes to JSON using its built-in type adapters. If <a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a>'s default JSON conversion isn't appropriate for a type, extend this class to customize the conversion. Here's an example of a type adapter for an (X,Y) coordinate point: </p><pre><div class="fragment"><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>PointAdapter <span class="keyword">extends</span> TypeAdapter&lt;Point&gt; {</div>
<div class="line">  <span class="keyword">public</span> Point read(JsonReader reader) <span class="keywordflow">throws</span> IOException {</div>
<div class="line">    <span class="keywordflow">if</span> (reader.peek() == JsonToken.NULL) {</div>
<div class="line">      reader.nextNull();</div>
<div class="line">      <span class="keywordflow">return</span> null;</div>
<div class="line">    }</div>
<div class="line">    String xy = reader.nextString();</div>
<div class="line">    String[] parts = xy.split(<span class="stringliteral">&quot;,&quot;</span>);</div>
<div class="line">    <span class="keywordtype">int</span> x = Integer.parseInt(parts[0]);</div>
<div class="line">    <span class="keywordtype">int</span> y = Integer.parseInt(parts[1]);</div>
<div class="line">    <span class="keywordflow">return</span> <span class="keyword">new</span> Point(x, y);</div>
<div class="line">  }</div>
<div class="line">  <span class="keyword">public</span> <span class="keywordtype">void</span> write(JsonWriter writer, Point value) <span class="keywordflow">throws</span> IOException {</div>
<div class="line">    <span class="keywordflow">if</span> (value == null) {</div>
<div class="line">      writer.nullValue();</div>
<div class="line">      <span class="keywordflow">return</span>;</div>
<div class="line">    }</div>
<div class="line">    String xy = value.getX() + <span class="stringliteral">&quot;,&quot;</span> + value.getY();</div>
<div class="line">    writer.value(xy);</div>
<div class="line">  }</div>
<div class="line">} </div>
</div><!-- fragment --> </pre><p> With this type adapter installed, <a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a> will convert</p><div class="fragment"><div class="line">Points </div>
</div><!-- fragment --><p> to JSON as strings like</p><div class="fragment"><div class="line"><span class="stringliteral">&quot;5,8&quot;</span> </div>
</div><!-- fragment --><p> rather than objects like</p><div class="fragment"><div class="line">{<span class="stringliteral">&quot;x&quot;</span>:5,<span class="stringliteral">&quot;y&quot;</span>:8} </div>
</div><!-- fragment --><p> . In this case the type adapter binds a rich Java class to a compact JSON value.</p>
<p>The <a class="el" href="">read()</a> method must read exactly one value and <a class="el" href="">write()</a> must write exactly one value. For primitive types this is means readers should make exactly one call to </p><div class="fragment"><div class="line">nextBoolean() </div>
</div><!-- fragment --><p> ,</p><div class="fragment"><div class="line">nextDouble() </div>
</div><!-- fragment --><p> ,</p><div class="fragment"><div class="line">nextInt() </div>
</div><!-- fragment --><p> ,</p><div class="fragment"><div class="line">nextLong() </div>
</div><!-- fragment --><p> ,</p><div class="fragment"><div class="line">nextString() </div>
</div><!-- fragment --><p> or</p><div class="fragment"><div class="line">nextNull() </div>
</div><!-- fragment --><p> . Writers should make exactly one call to one of <code>value()</code> or <code>nullValue()</code>. For arrays, type adapters should start with a call to</p><div class="fragment"><div class="line">beginArray() </div>
</div><!-- fragment --><p> , convert all elements, and finish with a call to</p><div class="fragment"><div class="line">endArray() </div>
</div><!-- fragment --><p> . For objects, they should start with</p><div class="fragment"><div class="line">beginObject() </div>
</div><!-- fragment --><p> , convert the object, and finish with</p><div class="fragment"><div class="line">endObject() </div>
</div><!-- fragment --><p> . Failing to convert a value or converting too many values may cause the application to crash.</p>
<p>Type adapters should be prepared to read null from the stream and write it to the stream. Alternatively, they should use <a class="el" href="">nullSafe()</a> method while registering the type adapter with <a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a>. If your</p><div class="fragment"><div class="line">Gson </div>
</div><!-- fragment --><p> instance has been configured to <a class="el" href="classcom_1_1google_1_1gson_1_1_gson_builder.html#a714da36aa74c4b5863beb44fc3197104">GsonBuilder#serializeNulls()</a>, these nulls will be written to the final document. Otherwise the value (and the corresponding name when writing to a JSON object) will be omitted automatically. In either case your type adapter must handle null.</p>
<p>To use a custom type adapter with <a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a>, you must <em>register</em> it with a <a class="el" href="classcom_1_1google_1_1gson_1_1_gson_builder.html">GsonBuilder</a>: </p><pre><div class="fragment"><div class="line">GsonBuilder builder = <span class="keyword">new</span> GsonBuilder();</div>
<div class="line">builder.registerTypeAdapter(Point.class, <span class="keyword">new</span> PointAdapter());</div>
<div class="line"><span class="comment">// if PointAdapter didn&#39;t check for nulls in its read/write methods, you should instead use</span></div>
<div class="line"><span class="comment">// builder.registerTypeAdapter(Point.class, new PointAdapter().nullSafe());</span></div>
<div class="line">...</div>
<div class="line">Gson gson = builder.create();</div>
</div><!-- fragment --> </pre><dl class="section since"><dt>自从</dt><dd>2.1 </dd></dl>
</div><hr/>该类的文档由以下文件生成:<ul>
<li>src/com/google/gson/<a class="el" href="_type_adapter_8java.html">TypeAdapter.java</a></li>
</ul>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
生成于 2014年 十一月 6日 星期四 11:22:04 , 为 AndBase开发框架使用  &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.8
</small></address>
</body>
</html>
